package easy

import "strings"

func ReverseWords(s string) string {
	words := strings.Fields(s)
	var sb strings.Builder
	for i := 0; i < len(words); i++ {
		chars := []byte(words[i])
		words[i] = reverseString(chars)
	}
	for i := 0; i < len(words); i++ {
		sb.WriteString(words[i])
		if i < len(words)-1 {
			sb.WriteByte(' ')
		}
	}
	return sb.String()
}

func reverseString(chars []byte) string {
	start := 0
	end := len(chars) - 1
	for start < end {
		chars[start], chars[end] = chars[end], chars[start]
		start++
		end--
	}
	return string(chars)

}
